package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.PageBase;
import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.DepartmentQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    public IDepartmentService departmentService;

    /**
     * 添加对象
     *
     * @param department 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult save(@RequestBody Department department) {//@RequestBody可以接收对象
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "添加失败" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败" + e.getMessage());
        }
    }

    @PatchMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Long> id) {
        try {
            departmentService.deleteAll(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败" + e.getMessage());
        }
    }

    /**
     * 修改对象
     *
     * @return Ajaxresult转换结果
     */
    @PostMapping
    public AjaxResult update(@RequestBody Department department) {
        try {
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "修改失败" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("{id}")
    public Department findById(@PathVariable("id") Long id) {
        return departmentService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @PatchMapping
    public PageBase<Department> findAll(@RequestBody DepartmentQuery query) {

        return departmentService.findAll(query);
    }

    /**
     * 查询所有部门
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }
}